import React, { Component } from 'react';
import { uuid, request } from '../util/commons-tools';
import Image from './Image';
import Video from './Video';
import Pdf from './Pdf';
import Rate from './Rate';
import ExchangeRate from './ExchangeRate';
import Error from './Error';
import Config from './Config';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      config: false,
      data: [],
      currentMedia: {}
    };
  }

  componentDidMount() {
    this.fetchSelData()
    // setTimeout(()=>{
    //   // console.log(fs)
    // //   var filePath = 'input.txt';
    // //   fs.watch(filePath, function (event, filename) {
    // //         console.log('event is: ' + event);
    // //         if (filename) {
    // //            console.log('filename provided: ' + filename);
    // //           //readTxt();
    // //         } else {
    // //           console.log('filename not provided');
    // //         }
    // //     // }
    // //   });
    // //   console.log(filePath + ' 被监听中...');
    // },1000)
    
  }

  fetchSelData = (id) => {
    let dataList = []
    request('downloadFile/modules.json', {}, (data) => {
      console.log(data)
      data.forEach(item => {
        request(`downloadFile/${item.Id}.json`, {}, (arr) => {
          dataList = dataList.concat(arr)
          arr.forEach(item => {
            if (item.resourceId === "4" || item.resourceId === "5") {
              request(`downloadFile/${item.fileName}.json`, {}, (list) => {
                item.list = list
              })
            }
          });
        })
      });
      setTimeout(() => {
        console.log(dataList, "数据")
        this.data = dataList;
        this.playMedia(dataList, 0, id)
      }, 500)
    })

  }
  // 是否在播放时间段
  isPlayTime = (mediaList, index) => {
    console.log(index)
    let media = mediaList[index];
    console.log(media.effectTime)
    if (media.effectTime === "0") {
      return media;
    } else {
      if (index < mediaList.length) {
        console.log(new Date() < new Date(media.endTime) && new Date() > new Date(media.startTime))
        if (new Date() < new Date(media.endTime) && new Date() > new Date(media.startTime)) {
          return media;
        } else {
          if (index < mediaList.length - 1) {
            return this.isPlayTime(mediaList, index + 1)
          } else {
            return this.isPlayTime(mediaList, 0)
          }
        }
      } else {
        return this.isPlayTime(mediaList, 0)
      }
    }
  }
  // 播放文件
  playMedia(mediaList, index, id) {
    console.log(index)
    let media = this.isPlayTime(mediaList, index);
    // let media = mediaList[index]
    if (media) {
      if (id && media.Id === id) {
        //解决只有一个多页PDF时，不轮播
        media.Id = uuid();
      }
      this.index = index;
      this.length = mediaList.length;
      this.setState({ "currentMedia": media });
    }
  }
  // 播放下一个文件
  startNextMedia = id => {
    console.log(this.index, this.length, id)
    if (this.index + 1 >= this.length) {
      this.data.some(v => v.resourceId === "5") ? this.fetchSelData(id) : this.playMedia(this.data, 0, id)
    } else {
      this.playMedia(this.data, this.index + 1);
    }
  }
  handleHome = val => {
    this.setState({
      config: val,
    })
    this.fetchSelData()
  }
  render() {
    // console.log(fs)
    let player = null;
    if (this.state.config) {
      player = <div>
        <Config isConfig={this.handleHome} />
      </div>
    } else {
      if (Object.keys(this.state.currentMedia).length > 0) {
        switch (this.state.currentMedia.resourceId) {
          case "3":
            player = <div>
              <Image
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
            break;
          case "2":
            player = <div>
              <Video
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
            break;
          case "66":
            player = <div>
              <Pdf
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
            break;
          case "4": //利率
            player = <div>
              <Rate
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
            break;
          case "5": // 汇率 
            player = <div>
              <ExchangeRate
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
            break;
          default:
            player = <div>
              <Error
                media={this.state.currentMedia}
                startNextMedia={this.startNextMedia}
              />
            </div>
        }
      }
    }

    return (
      <div>
        {player}
      </div>
    );
  }
}

export default App;
